/*
 * GenericDAOSpring.java
 *
 * Created on October 3, 2007, 11:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package petstore.persistence.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import petstore.model.BusinessObject;
import petstore.persistence.IGenericDAO;

/**
 *
 * @author Tom Hayden
 */
public class GenericDAOSpring extends JpaDaoSupport implements IGenericDAO {
    
    public Integer insert(BusinessObject businessObject) {
        this.getJpaTemplate().persist(businessObject);
        this.getJpaTemplate().flush();
        this.getJpaTemplate().refresh(businessObject);
        return businessObject.getId();
    }

    public Integer update(BusinessObject businessObject) {
        this.getJpaTemplate().merge(businessObject);
        return businessObject.getId();
    }

    public void remove(BusinessObject businessObject) {
        businessObject = this.getJpaTemplate().merge(businessObject);
        this.getJpaTemplate().remove(businessObject);
    }
    
    public BusinessObject findById(Class clazz, Integer id) {
        return (BusinessObject) this.getJpaTemplate().find(clazz, id);
    }
    
    public List findByDescription(Class clazz, String description) {
        Map params = new HashMap();
        params.put("description", description+"%");
        return getJpaTemplate().findByNamedParams("SELECT bo " +
                                                  "FROM "+clazz.getSimpleName()+" AS bo " +
                                                  "WHERE bo.description LIKE :description " +
                                                  "ORDER BY bo.description", params);
    }
    
    public List findByCriteria(String queryString) {
        return getJpaTemplate().find(queryString);
    }
    
    public List findByCriteria(String queryString, Map params) {
        return getJpaTemplate().findByNamedParams(queryString, params);
    }
    
    public List findByNamedQuery(String namedQuery) {
        return getJpaTemplate().findByNamedQuery(namedQuery);
    }
    
    public List findByNamedQuery(String queryString, Map params) {
        return getJpaTemplate().findByNamedQueryAndNamedParams(queryString, params);
    }
    
    public List findAll(Class clazz) {
        return this.getJpaTemplate().find("SELECT bo FROM "+clazz.getSimpleName()+" AS bo ");
    }

    public List executeNativeQuery(String queryString) {
        EntityManager em = null;
        List result = null;
        try {
            em = getJpaTemplate().getEntityManagerFactory().createEntityManager();
            Query query = em.createNativeQuery(queryString);
            result = query.getResultList();
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
        return result;
    }
    
}
